//
//  TPViewController.swift
//  Auscultation
//
//  Created by zwb on 2021/3/31.
//

import UIKit

class TPViewController: BaseVC {
    
    @IBOutlet weak var table: UITableView!
    @IBOutlet weak var topContraint: NSLayoutConstraint!
    var pageNum: Int = 1
    var pageSize: Int = 10
    var dataArray: [TPListModel?] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let top_view = UIImageView()
        top_view.image = UIImage(named: "table2")
        top_view.isUserInteractionEnabled = true
        self.view.addSubview(top_view)
        top_view.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(UIDevice.lgf_NavBarH())
        }
        let style = LGFNavigationBarStyle.lgf()
        style.lgf_LeftBtnImageLight = UIImage(named: "darkback")!
        self.lgf_NavigationBar.lgf_line?.isHidden = true

        self.lgf_NavigationBar.lgf_Reload()

        self.topContraint.constant = UIDevice.lgf_NavBarH()
        self.navTitle = "培训大纲"
        
        table.register(UINib.init(nibName: "TPListTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "TPListTableViewCell")
        table.estimatedRowHeight = 55
        // 自动适应cell高度
        table.rowHeight = UITableView.automaticDimension
        loadData()
        table.mj_header = MJRefreshNormalHeader.init(refreshingBlock: { [weak self] in
            self?.pageNum = 1
            self?.loadData()

        })
        
//        table.mj_footer = MJRefreshBackNormalFooter.init(refreshingBlock: {
//            [weak self] in
//            self?.pageNum += 1
//            self?.loadData()
//
//        })
        
        table.tableFooterView = UIView()
    }

}

extension TPViewController:UITableViewDelegate,UITableViewDataSource{
    
    // MARK: - UITableViewDataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TPListTableViewCell") as! TPListTableViewCell
        guard let model = dataArray[indexPath.row] else { return cell }
        cell.model = model
        return cell

    }
    
    // MARK: - UITableViewDelegate
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       
        table.deselectRow(at: indexPath, animated: false)
        let vc = TPContentVC()
        vc.model = self.dataArray[indexPath.row]
        vc.dataSource = self.dataArray[indexPath.row]?.children
        self.navigationController?.pushViewController(vc, animated: true)
    }
//    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        
//        return 50
//    }
}


extension TPViewController{

    func loadData() {
        
        let paramet  = [
            "pageNo" : pageNum.description,
            "pageSize" : pageSize.description,
            "encryptedData": "zFoamlSX95gBI7S05+e6HakuGltzhbckTNdkY0yZ2hU="
        ]
        
        EWERequest.lgf("3dStudy/course", paramet, .get) {  (data) in
            
            guard let result = (data as? [Any]) else { return  }
            if self.pageNum == 1 {
                self.dataArray.removeAll()
                self.table.mj_header?.endRefreshing()
            }
            
            let arr = Array<TPListModel>.deserialize(from: result)
            if arr?.count == 0 {
                self.table.mj_footer?.endRefreshingWithNoMoreData()
            }else{
                self.table.mj_footer?.endRefreshing()
            }
            self.dataArray += arr ?? []
            self.table.reloadData()
        } _: {  (error) in
            SVProgressHUD.showError(error)
        }

    }
    
}
